import axios from 'axios'

const API_URL = '/api/v1'

// 创建axios实例
const apiClient = axios.create({
  baseURL: API_URL,
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  },
  timeout: 10000
})

// 请求拦截器
apiClient.interceptors.request.use(
  config => {
    console.log(`发送请求: ${config.method.toUpperCase()} ${config.url}`, config.data || {})
    return config
  },
  error => {
    console.error('请求错误:', error)
    return Promise.reject(error)
  }
)

// 响应拦截器
apiClient.interceptors.response.use(
  response => {
    console.log(`响应数据:`, response.data)
    return response
  },
  error => {
    console.error('响应错误:', error.response || error)
    return Promise.reject(error)
  }
)

export const analyticsApi = {
  async getTaskPerformance(days = 30) {
    try {
      return await apiClient.get(`/analytics/task-performance?days=${days}`)
    } catch (error) {
      console.error('获取任务性能指标失败:', error)
      throw error
    }
  },

  async getCombinedAnalytics() {
    try {
      return await apiClient.get('/analytics/combined')
    } catch (error) {
      console.error('获取综合分析数据失败:', error)
      throw error
    }
  },

  async getClickhouseStats() {
    try {
      return await apiClient.get('/analytics/clickhouse-stats')
    } catch (error) {
      console.error('获取 ClickHouse 统计数据失败:', error)
      throw error
    }
  },

  async triggerSync() {
    try {
      return await apiClient.post('/analytics/sync')
    } catch (error) {
      console.error('触发数据同步失败:', error)
      throw error
    }
  },

  async generateReport(days = 30, format = 'json') {
    try {
      return await apiClient.get(`/analytics/report?days=${days}&format=${format}`)
    } catch (error) {
      console.error('生成分析报告失败:', error)
      throw error
    }
  }
} 